package core;

import org.kxml2.kdom.*; 

public class ScheduleEntry {
	
	private Subject m_subject;
	private WeekDefinition m_week;
	private DayOfWeek m_day;
	private TimeOfTheClasses m_time;

	public ScheduleEntry(DayOfWeek dayOfWeek, WeekDefinition week, TimeOfTheClasses time, Subject subject) {
		m_day = dayOfWeek;
		m_week = week;
		m_time = time;
		m_subject = subject;
	}
	
	public DayOfWeek getDayOfWeek(){
		return m_day;
	}
	
	public void setDayOfWeek(DayOfWeek day){
		m_day = day;
	}
	
	public WeekDefinition getWeek(){
		return m_week;
	}
	
	public TimeOfTheClasses getTime(){
		return m_time;
	}
	
	public Subject getSubject(){
		return m_subject;
	}
	
	public void toXml(Node node) throws Throwable{
		
		try {
	        Element root = node.createElement(null, "SheduleEntry");
	        
            m_day.toXml(root);
            m_week.toXml(root);
            m_subject.toXml(root);
            m_time.toXml(root);
            node.addChild(Node.ELEMENT, root);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static ScheduleEntry fromXml(Element node) throws Throwable{
		try
	    {
			Element rootElem = node; 
			if (!rootElem.getName().equals("SheduleEntry")){
				rootElem = node.getElement(null, "SheduleEntry");
			}
			
			Subject subject = Subject.fromXml(rootElem);
			WeekDefinition week = WeekDefinition.fromXml(rootElem);
			DayOfWeek day = DayOfWeek.fromXml(rootElem);
			TimeOfTheClasses time = TimeOfTheClasses.fromXml(rootElem);

			ScheduleEntry entry = new ScheduleEntry(day, week, time, subject);
			
			return entry;
	    }
		catch (Exception e) {
			throw e;
		}
	}

}
